package a_o2odata_deal

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext
import org.elasticsearch.spark._

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2019/11/28 15:30
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object yushou_v1_main {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    //conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    val yushou_data = sqlContext.read.json("s3a://o2o-dataproces-group/panzonghao/last_yushou_month11")
    //加载预售ES数据
    val rdd = sc.esJsonRDD("2019_presale_tmall/tmall_2019_shuang11_4").values
    val es_data = sqlContext.read.json(rdd)
    es_data.createOrReplaceTempView("es_data")
    yushou_data.createOrReplaceTempView("yushou_data")

    import org.apache.spark.sql.functions._
    val result = sqlContext.sql(
      s"""
         |select
         |*,
         |(cast(salesAmount as double)/cast(sellCount as double))*cast(sells as double) as sales
         |from
         |(select
         |t1.*,
         |t2.preSaleCount as sells
         |from
         |es_data t1
         |inner join
         |yushou_data t2
         |on t1.good_id=t2.good_id)
       """.stripMargin).drop("sellCount","salesAmount","salesAmountAre","priceTextAre")
      .withColumnRenamed("sells","sellCount")
      .withColumnRenamed("sales","salesAmount")
      .withColumn("extendedData",lit("-1"))
      .withColumn("preSaleCount",lit("-1"))
      .withColumn("earnest",lit("-1"))

    result.repartition(4).write.json("s3a://o2o-dataproces-group/panzonghao/last_yushou_month11_result")

  }

}
